package com.mindainfo.groundwave.server.member.dao.impl;

import com.mindainfo.groundwave.server.base.dao.impl.BaseDaoImpl;
import com.mindainfo.groundwave.server.member.bean.Staffer;
import com.mindainfo.groundwave.server.member.dao.IStafferDao;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by MD-01 on 2017/5/27.
 */
@Repository
public class StafferDaoImpl extends BaseDaoImpl<Staffer> implements IStafferDao {
    @Override
    public List<Staffer> queryList(int start, int num, List<Integer> deptId, String key) {

        Query<Staffer> query = getDatastore().createQuery(Staffer.class);
        if(deptId!=null){
            query.field("deptId").in(deptId);
        }
        if(!StringUtils.isEmpty(key)){
            query.or(query.criteria("name").contains(key));
        }
        FindOptions options = new FindOptions();
        options.skip(start).limit(num);

        return query.order("-createTime").asList(options);
    }

    @Override
    public int queryListCount(List<Integer> deptId, String key) {
        Query<Staffer> query = getDatastore().createQuery(Staffer.class);
        if(deptId!=null){
            query.field("deptId").in(deptId);
        }
        if(!StringUtils.isEmpty(key)){
            query.or(query.criteria("name").contains(key));
        }
        return (int) query.count();
    }

    @Override
    public List<Staffer> queryUsableAll() {
        return getDatastore().createQuery(Staffer.class)
                .asList();
    }

    @Override
    public List<Staffer> queryByDeptId(Integer deptId) {
        return getDatastore().createQuery(Staffer.class)
                .field("deptId").equal(deptId)
                .asList();
    }
}
